package org.model.data;

/*import superwaba.ext.xplat.io.DataStream;
import waba.io.Catalog;
import waba.io.IOError;
import waba.io.ResizeRecord;
import waba.sys.Settings;
*/import java.util.Vector;

/**
 *
 * @author German Garcia
 */
public class DataManager {
    private static final String CREATOR_ID = "Gt&d"; //Settings.appCreatorId;
    public final static String SETUP_CATALOG = "setup." + CREATOR_ID + ".info";
    public final static String DEVICES_CATALOG = "device." + CREATOR_ID + ".info";
    public final static String DATA_CATALOG = "data." + CREATOR_ID + ".info";

/*    private Catalog catalog;
    private ResizeRecord record;
    private DataStream stream;*/

    private DataManager(String catalogName) {
/*    	Settings.useExceptions=true;
    	this.catalog = new Catalog(catalogName, Catalog.CREATE);
        if(this.catalog.isOpen()) {
            // create the streams
            this.record = new ResizeRecord(this.catalog, 512); // 512 is the initial size for each record. Cannot be 0!
            this.stream = new DataStream(record);
        }*/
    }

    public static DataManager getInstance(String catalogName) {
        if(catalogName.equalsIgnoreCase(SETUP_CATALOG)) catalogName = SETUP_CATALOG;
        else if(catalogName.equalsIgnoreCase(DEVICES_CATALOG)) catalogName = DEVICES_CATALOG;
        else if(catalogName.equalsIgnoreCase(DATA_CATALOG)) catalogName = DATA_CATALOG;
        else return null;
        DataManager dm = new DataManager(catalogName);
        if(dm.isValid()) return dm;
        return null;
    }

    public void releaseInstance() {
        //if(catalog.isOpen()) catalog.close();
    }

    public static boolean removeCatalog(String catalogName) {
        boolean retval = false;
/*        DataManager dm = getInstance(catalogName);
        if(dm.catalog.isOpen()) retval = dm.catalog.delete();
*/        return retval;
    }

    public boolean isValid() {
        //return catalog.isOpen();
    	return true; //comando temporal,hay que ajustar
    }

    public boolean existsDevice(Device device) {
/*        if(catalog.isOpen()) {
            String id = null;
            int count = catalog.getRecordCount();
            try {
                for(int i=0; i<count; i++) {
                    if(catalog.setRecordPos(i)) {
                        id = stream.readString();
                        if(device.getId().equalsIgnoreCase(id)) return true;
                    }
                }
            } catch (IOError e) {
                e.printStackTrace();
            }
        }
*/        return false;        
    }

    public boolean saveDevice(Device device) {
/*        if(catalog.isOpen()) {
            //if(!existsDevice(device)) {
                try {
                    if(record.startRecord()) {
                        stream.writeString(device.getId());
                        stream.writeString(device.getName());
                        stream.writeInt(device.getType());
                        stream.writeString(device.getReference());
                        record.endRecord();
                        return true;
                    }
                } catch (IOError e) {
                    e.printStackTrace();
                }
            //}
            if(!existsDevice(device)) {
                byte[] record = device.getAsString().getBytes();
                catalog.addRecord(record.length);
                catalog.writeBytes(record, 0, record.length);
                return true;
            }
        }
*/        return false;
    }

    public boolean deleteDevice(Device device) {
/*        if(catalog.isOpen()) {
            String id = null;
            int count = catalog.getRecordCount();
            try {
                for(int i=0; i<count; i++) {
                    if(catalog.setRecordPos(i)) {
                        id = stream.readString();
                        if(device.getId().equalsIgnoreCase(id)) {
                            return catalog.deleteRecord();
                        }
                    }
                }
            } catch (IOError e) {
                e.printStackTrace();
            }
        }*/
        return false;
    }

    public Device[] listDevices() {
        Device[] devices = null;
        //if(catalog.isOpen()) {
            int count = 1;//catalog.getRecordCount();
            devices = new Device[count];
            try {
                for(int i=0; i<count; i++) {
                    //if(catalog.setRecordPos(i)) {
                        devices[i] = new Device();
                        devices[i].setId("2");//(stream.readString());
                        devices[i].setName("bymedia");//(stream.readString());
                        devices[i].setType(1);//(stream.readInt());
                        devices[i].setReference("GSM");//(stream.readString());
                    //}
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        //}
        return devices;
    }

    public Device[] GLESList() {
        Device[] gles = null;
        Device[] devices = listDevices();
        if(devices!=null) {
            Vector vector = new java.util.Vector();
            for(int i=0; i<devices.length; i++)
                if(devices[i].getType()==0) // 0 es GLES, 1 es medidor
                    vector.addElement(devices[i]);
            gles = new Device[vector.size()];
            for(int i=0; i<vector.size(); i++)
                gles[i] = (Device)vector.elementAt(i); //items[i];
        }
        return gles;
    }

    public Device[] metersList() {
        Device[] meters = null;
        Device[] devices = listDevices();
        if(devices!=null) {
            Vector vector = new Vector();
            for(int i=0; i<devices.length; i++)
                if(devices[i].getType()==1) // 0 es GLES, 1 es medidor
                    vector.addElement(devices[i]);
            meters = new Device[vector.size()];
            for(int i=0; i<vector.size(); i++)
                meters[i] = (Device)vector.elementAt(i);// items[i];
        }
        return meters;
    }

    public boolean saveConfiguration(Configuration configuration) {
/*        if(catalog.isOpen()) {
            int count = catalog.getRecordCount();
            if(count>0) {
                try {
                    if(record.restartRecord(0)) {
                        stream.writeString(configuration.getComPort());
                        stream.writeString(configuration.getLicense());
                        stream.writeString(configuration.getRefresh());
                        stream.writeString(configuration.getServer1());
                        stream.writeString(configuration.getServer2());
                        stream.writeBoolean(configuration.isAutoMonitor());
                        record.endRecord();
                        return true;
                    }
                } catch (IOError e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    if(record.startRecord()) {
                        stream.writeString(configuration.getComPort());
                        stream.writeString(configuration.getLicense());
                        stream.writeString(configuration.getRefresh());
                        stream.writeString(configuration.getServer1());
                        stream.writeString(configuration.getServer2());
                        stream.writeBoolean(configuration.isAutoMonitor());
                        record.endRecord();
                        return true;
                    }
                } catch (IOError e) {
                    e.printStackTrace();
                }
            }
        }*/
        return false;
    }

    public Configuration readConfiguration() {
        Configuration configuration = null;
/*        if(catalog.isOpen()) {
            int count = catalog.getRecordCount();
            if(count>0) {
                try {
                    catalog.setRecordPos(0);
                    configuration = new Configuration();
                    configuration.setComPort(stream.readString());
                    configuration.setLicense(stream.readString());
                    configuration.setRefresh(stream.readString());
                    configuration.setServer1(stream.readString());
                    configuration.setServer2(stream.readString());
                    configuration.setAutoMonitor(stream.readBoolean());
                    //System.out.println(configuration.getAsString());
                } catch (IOError e) {
                    e.printStackTrace();
                }
            }
        }
*/        
        //PARA PROBAR COLOCO ESTA CONFIGURACION DE PRUEBA
        configuration = new Configuration();
        configuration.setComPort("3");
        configuration.setLicense("bymedia-1");
        configuration.setRefresh("2 Minutes");
        configuration.setServer1("72.167.147.5:8070");
        configuration.setServer2("72.167.147.5:8070");
        configuration.setAutoMonitor(false);
        
        return configuration;
    }

    public boolean saveData(String data[]) {
/*        if(catalog.isOpen()) {
            try {
                for(int i=0; i<data.length; i++)
                    if(data[i].indexOf("error")<0)
                    if(record.startRecord()) {
                        stream.writeString(data[i]);
                        record.endRecord();
                    }
                return true;
            } catch (IOError e) {
                e.printStackTrace();
            }
        }
*/        return false;
    }

    public String[] getSavedData() {
        String[] data = null;
/*        if(catalog.isOpen()) {
            int count = catalog.getRecordCount();
            if(count>0) {
                data = new String[count];
                try {
                    for(int i=0; i<count; i++) {
                        if(catalog.setRecordPos(i)) data[i] = stream.readString();
                    }
                } catch (IOError e) {
                    e.printStackTrace();
                }
            }
        }
*/        return data;
    }

    public String[] get32SavedData() {
        String[] data = null;
/*        if(catalog.isOpen()) {
            int count = catalog.getRecordCount(), size = 32;
            if(count>0) {
                if(count<32) size = count;
                data = new String[size];
                try {
                    for(int i=0; i<size; i++) {
                        if(catalog.setRecordPos(0)) {
                            data[i] = stream.readString();
                            catalog.deleteRecord();
                        }
                    }
                } catch (IOError e) {
                    e.printStackTrace();
                }
            }
        }
*/        return data;
    }

    public void clearSavedData() {
/*        if(catalog.isOpen()) {
            while(catalog.getRecordCount()>0) {
                catalog.setRecordPos(0);
                catalog.deleteRecord();
            }
        }*/
    }
}
